﻿using System;
using System.Windows.Data;
using System.Activities.Presentation.Toolbox;
using System.Windows.Media;
using System.ComponentModel;
using System.Activities.Presentation;
using System.Linq;

namespace Vision.VisionWorkshop.Common
{
    public class ToolboxItemWrapperIconConverter : IValueConverter
    {
        /// <summary>
        /// Defines the default icon resource to use when none is found
        /// </summary>
        public string DefaultResource { get; set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Drawing retVal = null;

            ToolboxItemWrapper wrapper = value as ToolboxItemWrapper;

            if (null != wrapper){
                // Generate the resource name
                string resourceName = GenerateResourceName(wrapper.Type);

                // And lookup in app resources
                DrawingBrush icon = System.Windows.Application.Current.Resources[resourceName] as DrawingBrush;
                
                // No icon found, now try the designer - this is really the last resort
                if (null == icon){
                    // Get the [Designer] for the passed activity
                    DesignerAttribute designer = Attribute.GetCustomAttribute(wrapper.Type, typeof(DesignerAttribute)) as DesignerAttribute;
                    if (null != designer){
                        ActivityDesigner ad = Activator.CreateInstance(Type.GetType(designer.DesignerTypeName)) as ActivityDesigner;
                        if (null != ad){
                            icon = ad.Icon;
                        }
                    }
                }

                // If not found, provide a fallback
                if (null == icon){
                    icon = System.Windows.Application.Current.Resources[DefaultResource] as DrawingBrush;
                }

                if (null != icon){
                    retVal = icon.Drawing;
                }
            }

            return retVal;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        private string GenerateResourceName(Type t)
        {
            string resource = this.DefaultResource;

            if (t.IsGenericType)
            {
                resource = string.Concat(t.Name.Substring(0, t.Name.IndexOf('`')), "Icon");
            }
            else
            {
                switch (t.Name)
                {
                    case "Flowchart":
                        {
                            resource = "FlowChartIcon";
                            break;
                        }
                    case "TransactedReceiveScope":
                        {
                            resource = "TransactionReceiveScopeIcon";
                            break;
                        }
                    default:
                        {
                            resource = string.Concat(t.Name, "Icon");
                            break;
                        }
                }
            }

            return resource;
        }
    }    
}
